package oop.ex3.tester;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class FakeFM {
	public static void main(String[] args) throws UnknownHostException, IOException{
		System.out.println("%%%%% NEW FAKE FM %%%%%");
		System.out.println("FakeFM Initiated...");
		Socket myClient = new Socket("127.0.0.1",1337);
		System.out.println("FakeFM connected? "+myClient.isConnected());
		System.out.println("FakeFM connected to: "
					+myClient.getInetAddress()+":"+myClient.getPort());
		DataInputStream input = new DataInputStream(myClient.getInputStream());
		
		DataOutputStream output = new DataOutputStream(myClient.getOutputStream());
		//while( some condition ) {
		output.writeUTF("Begin");
		output.writeUTF("13.37.13.37");
		output.writeInt(1337);
		output.writeUTF("END");
		
		String incoming = input.readUTF();
		System.out.println(incoming);
		System.out.println(input.readUTF());
		if (incoming.equals("WELCOME")){
		output.writeUTF("CONTAINFILE");
		output.writeUTF("fileA");
		output.writeUTF("END");
		System.out.println("A sent, response: ");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		output.writeUTF("CONTAINFILE");
		output.writeUTF("fileB");
		output.writeUTF("END");
		System.out.println("B sent, response: ");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		
		output.writeUTF("CONTAINFILE");
		output.writeUTF("fileC");
		output.writeUTF("END");
		System.out.println("C sent, response: ");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		
		output.writeUTF("ENDLIST");
		output.writeUTF("END");
		System.out.println("END OF LIST, response: ");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		
		output.writeUTF("CONTAINNAMESERVER");
		output.writeUTF("1.2.3.4");
		output.writeInt(1234);
		output.writeUTF("END");
		System.out.println("1.2.3.4%1234 sent, response: ");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		output.writeUTF("CONTAINNAMESERVER");
		output.writeUTF("5.6.7.8");
		output.writeInt(5678);
		output.writeUTF("END");
		System.out.println("5.6.7.8%5678 sent, response: ");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		output.writeUTF("CONTAINNAMESERVER");
		output.writeUTF("9.10.11.12");
		output.writeInt(9111);
		output.writeUTF("END");
		System.out.println("9.10.11.12:9111 sent, response: ");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		
		output.writeUTF("ENDLIST");
		output.writeUTF("END");
		System.out.println("END OF LIST, response: ");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		
		
		}
		// process input
		//output.println(outgoing message);
		//}
		
		output.writeUTF("DONTCONTAINFILE");
		output.writeUTF("fileB");
		output.writeUTF("END");
		System.out.println("REMOVED");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		//output.writeUTF("BLAT");
		output.writeUTF("CONTAINFILE");
		output.writeUTF("fileAB");
		output.writeUTF("END");
		System.out.println(input.readUTF());
		System.out.println(input.readUTF());
		
		
		input.close(); output.close(); myClient.close();
	}
}
